package com.zoke.loveway.view.indicator.transition;

import android.content.Context;
import android.content.res.Resources;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import com.zoke.loveway.view.indicator.Indicator.OnTransitionListener;

/**
 * 
 * 
 */
public class OnTransitionTextListener implements OnTransitionListener {
	private float selectSize = -1;
	private float unSelectSize = -1;
	private int selectColor = -1;
	private int unSelectColor = -1;
	private ColorGradient gradient;
	private float dFontFize = -1;

	private boolean isPxSize = false;

	public OnTransitionTextListener() {
		super();
	}

	public OnTransitionTextListener(float selectSize, float unSelectSize,
			int selectColor, int unSelectColor) {
		super();
		setColor(selectColor, unSelectColor);
		setSize(selectSize, unSelectSize);
	}

	public final OnTransitionTextListener setSize(float selectSize,
			float unSelectSize) {
		isPxSize = false;
		this.selectSize = selectSize;
		this.unSelectSize = unSelectSize;
		this.dFontFize = selectSize - unSelectSize;
		return this;
	}

	public final OnTransitionTextListener setValueFromRes(Context context,
			int selectColorId, int unSelectColorId, int selectSizeId,
			int unSelectSizeId) {
		setColorId(context, selectColorId, unSelectColorId);
		setSizeId(context, selectSizeId, unSelectSizeId);
		return this;
	}

	public final OnTransitionTextListener setColorId(Context context,
			int selectColorId, int unSelectColorId) {
		Resources res = context.getResources();
		setColor(res.getColor(selectColorId), res.getColor(unSelectColorId));
		return this;
	}

	public final OnTransitionTextListener setSizeId(Context context,
			int selectSizeId, int unSelectSizeId) {
		Resources res = context.getResources();
		setSize(res.getDimensionPixelOffset(selectSizeId),
				res.getDimensionPixelOffset(unSelectSizeId));
		isPxSize = true;
		return this;
	}

	public final OnTransitionTextListener setColor(int selectColor,
			int unSelectColor) {
		this.selectColor = selectColor;
		this.unSelectColor = unSelectColor;
		gradient = new ColorGradient(unSelectColor, selectColor, 100);
		return this;
	}

	/**
	 * 如果tabItemView 不是目标的TextView，那么你可以重写该方法返回实际要变化的TextView
	 * 
	 * @param tabItemView
	 *            Indicator的每一项的view
	 * @param position
	 *            view在Indicator的位置索引
	 * @return
	 */
	public TextView getTextView(View tabItemView, int position) {
		if (tabItemView instanceof TextView)
			return (TextView) tabItemView;
		return null;
	}

	float fontTemp;
	// 抛物线的系数a、b、c与dFontSize的比值（y=-a(x-b)^2 + c）
	private static final float factor_a = (float) ((2 + Math.sqrt(3)) / 2);
	private static final float b = (float) ((3 - Math.sqrt(3)) / 2);
	private static final float factor_c = 1.5f;

	@Override
	public void onTransition(View view, int position, float selectPercent) {
		TextView selectTextView = getTextView(view, position);
		if (selectColor != -1 && unSelectColor != -1) {
			selectTextView.setTextColor(gradient
					.getColor((int) (selectPercent * 100)));
		}
		if (unSelectSize > 0 && selectSize > 0) {
			// 优化字体变化的曲线 ---- 2015.04.28 shangzhi
			// fontTemp = unSelectSize + dFontFize * selectPercent;//原直线
			// 在80%时字体最大的效果，用抛物线拟合
			fontTemp = (float) (unSelectSize - dFontFize * factor_a
					* Math.pow((selectPercent - b), 2) + dFontFize * factor_c);
			if (isPxSize) {
				selectTextView
						.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontTemp);
			} else {
				selectTextView.setTextSize(fontTemp);
			}
		}
	}

}
